package com.srwl.mytx.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.srwl.mytx.R;
import com.srwl.mytx.utils.Utils;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TitleBar extends LinearLayout {

    private static final String TAG = "TopBar";
    private Context context;
    @BindView(R.id.status_bar_background)
    FrameLayout statusBarBackground;
    @BindView(R.id.title_bar_background)
    FrameLayout titleBarBackground;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.rl_left)
    RelativeLayout rl_left;
    @BindView(R.id.rl_right)
    RelativeLayout rl_right;
    @BindView(R.id.iv_back)
    MyImageView iv_back;
    //  @BindView(R.id.tv_title)
    MyTextView tv_title;
    @BindView(R.id.iv_right_menu)
    MyImageView iv_rightMenu;
    @BindView(R.id.place_holder)
    View placeHolder;
    private boolean hasNotch;//是否是刘海屏 水滴屏标记，如果是，那么不是全屏显示，用不着控制状态栏字体颜色
    private InteractionListener listener;

    public TitleBar(final Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {

        this.context = context;
        View view = inflate(context, R.layout.top_bar, this);
        tv_title = view.findViewById(R.id.tv_title);
        ButterKnife.bind(this);
        rl_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onLeftImageClick();
                }
            }
        });
        rl_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onRightImageClick();
                }
            }
        });
    }

    public void init(String title, InteractionListener listener) {
        tv_title.setText(title);
        this.listener = listener;
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }

    public void adaptVideo() {
        statusBarBackground.setVisibility(VISIBLE);
        int statusBarHeight = Utils.getStatusBarHeight(context);
        if (statusBarHeight == 0) {
            statusBarHeight = Utils.dpToPx(20, context); //如果没获取到 状态栏高度，按20dp 处理
        }
        ViewGroup.LayoutParams layoutParams = statusBarBackground.getLayoutParams();
        layoutParams.height = statusBarHeight;
        statusBarBackground.setLayoutParams(layoutParams);
        statusBarBackground.scrollTo(0, Utils.dpToPx(statusBarHeight, context));
        titleBarBackground.scrollTo(0, Utils.dpToPx(R.dimen.top_bar_height, context));//让白色滑动背景滑出可见区域
        tv_title.showEndColor();//显示白色
        iv_back.showEndColor();
        iv_rightMenu.showEndColor();
        placeHolder.setVisibility(INVISIBLE);//不能调用 showPlaceHolder（false）方法,估计是因为这时候 placeHolder 还没有绘制出现，所以isShow()总是返回true;
    }

    public void adaptVideoHasNotch() {
        hasNotch = true;
        titleBarBackground.scrollTo(0, Utils.dpToPx(R.dimen.top_bar_height, context));//让白色滑动背景滑出可见区域
        tv_title.showEndColor();//显示白色
        iv_back.showEndColor();
        iv_rightMenu.showEndColor();
        placeHolder.setVisibility(INVISIBLE);//不能调用 showPlaceHolder（false）方法,估计是因为这时候 placeHolder 还没有绘制出现，所以isShow()总是返回true;
    }


    /**
     * 设置状态栏字体颜色
     *
     * @param dark true 为深色
     */
    private void setStatusBarTextColorDark(boolean dark) {
        View decor = ((Activity) context).getWindow().getDecorView();
        if (dark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    /**
     * 删除视频后回显图片
     */
    public void updateTopBar() {
        statusBarBackground.scrollTo(0, 0);
        titleBarBackground.scrollTo(0, 0);//让白色滑动背景滑出可见区域
        tv_title.showStartColor();
        iv_back.showStartColor();
        iv_rightMenu.showStartColor();
        showPlaceHolder(true);


    }

    public void scrollTitleBarBackground(int scrollY, int screenHeight) {
        //播放器满屏显示，所以播放器高度=屏幕高度
        //overlap 为屏幕上滑过程中，视频播放器留在屏幕内（可见的）高度值
        int overlap = screenHeight - scrollY;
        if (overlap > getBottom() + statusBarBackground.getHeight()) {
            if (!hasNotch) {
                setStatusBarTextColorDark(false);
            }
            statusBarBackground.scrollTo(0, statusBarBackground.getHeight());//状态栏背景全部显示透明
            titleBarBackground.scrollTo(0, titleBar.getHeight());//标题栏背景全部显示透明
            tv_title.showEndColor();//title 字体全部显示白色
            iv_back.showEndColor();
            iv_rightMenu.showEndColor();
            showPlaceHolder(false);
        } else if (overlap <= getBottom() + statusBarBackground.getHeight() && overlap > getBottom()) {
            statusBarBackground.scrollTo(0, overlap - getBottom());//动态显示
            titleBarBackground.scrollTo(0, titleBar.getHeight());//标题栏背景全部显示透明
            tv_title.showEndColor();//title 字体全部显示白色
            iv_back.showEndColor();
            iv_rightMenu.showEndColor();
            showPlaceHolder(false);
        } else if (overlap <= titleBar.getBottom() && overlap >= titleBar.getTop()) {//判断播放器下边缘与 titleBar 下边缘接触，到播放器下边缘离开titleBar 上边缘的过程
            if (!hasNotch) {
                setStatusBarTextColorDark(true);
            }
            statusBarBackground.scrollTo(0, 0);//状态栏背景全部显示灰色
            //白色背景从titleBar上边缘移入或者移出
            titleBarBackground.scrollTo(0, (overlap - titleBar.getTop()));//动态显示

            if (overlap <= titleBar.getBottom() - tv_title.getTop() && overlap >= titleBar.getTop() + tv_title.getTop()) {

                float rate = new BigDecimal((float) (titleBar.getBottom() - tv_title.getTop() - overlap) / tv_title.getHeight()).setScale(2, BigDecimal.ROUND_FLOOR).floatValue();
                tv_title.setColorPosition(rate);//动态显示
                iv_back.setColorPosition(rate);
                iv_rightMenu.setColorPosition(rate);
            }
            showPlaceHolder(false);
        } else if (overlap < titleBar.getTop()) {//播放器下边缘在topBar 上边缘之上,也就是 播放器完全不可见，
            if (!hasNotch) {
                setStatusBarTextColorDark(true);
            }
            statusBarBackground.scrollTo(0, 0);//状态栏背景全部显示灰色
            titleBarBackground.scrollTo(0, 0);//标题栏背景全部显示白色
            tv_title.showStartColor();//title 字体全部显示黑色
            iv_back.showStartColor();
            iv_rightMenu.showStartColor();
            showPlaceHolder(true);
        }
    }


    private void showPlaceHolder(boolean isShow) {
        if (isShow && !placeHolder.isShown()) {
            placeHolder.setVisibility(VISIBLE);
        } else if (!isShow && placeHolder.isShown()) {
            placeHolder.setVisibility(INVISIBLE);
        }
    }

    public interface InteractionListener {

        void onLeftImageClick();

        void onRightImageClick();
    }

}
